package com.example.springbootServiceNetwork.demos.web.Controller;

import com.aliyuncs.exceptions.ClientException;
import com.example.springbootServiceNetwork.demos.web.Config.Result;
import com.example.springbootServiceNetwork.demos.web.Tool.SmsUtil;
import com.example.springbootServiceNetwork.demos.web.base.RedisDao;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import static com.example.springbootServiceNetwork.demos.web.Tool.RandomUtil.getSmsRandomNumber;
import static com.example.springbootServiceNetwork.demos.web.Tool.ToolUtil.isPhoneNumber;

@RestController
@RequestMapping("/tool")
public class ToolController {
    @Resource
    private SmsUtil smsUtil;
    @Resource
    private RedisDao redisDao;

    @Value("${redis.epxtime}")
    private Long epxtime;
    @Value("${redis.imaileEpxtime}")
    private Long imaileEpxtime;

    /**
     * 发送短信验证嘛
     */
    @GetMapping("/sendMessage")
    public Result sendMessage(@RequestParam("phone") String phone){
        Result result = new Result();
        String code = getSmsRandomNumber(); // 生成随机验证码
        try {
            if(isPhoneNumber(phone)){
                redisDao.vSet(code,code,imaileEpxtime);
                result = smsUtil.sendSms(phone, code);
            }else {
                result.setCode(400);
                result.setMsg("手机号格式错误！");
            }
        } catch (ClientException e) {
            e.fillInStackTrace();
            result.setCode(500);
            result.setMsg(e.getErrMsg());
        }
        return result;
    }

}
